//
//  MallShopTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MallShopTableViewCell.h"

@interface MallShopTableViewCell ()

Strong UIImageView *titleImage;
Strong UILabel *titleLabel;
Strong UILabel *rightLabel;
Strong UIView *lineView;

@end

@implementation MallShopTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _titleImage = [UIImageView new];
        _titleImage.contentMode = UIViewContentModeScaleAspectFit;
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 0;
        
        _rightLabel  = [[UILabel alloc]init];
        _rightLabel.font = DEF_FontSize_14;
        _rightLabel.textColor = getColor(textColor);
        _rightLabel.text = @"详情";
        
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:_lineView];
        [self.contentView sd_addSubviews:@[_titleLabel  , _titleImage , _rightLabel]];
        
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(67 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(67 / WIDTH_5S_SCALE);
    }];
    
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImage.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(87 / WIDTH_5S_SCALE);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 0 / WIDTH_5S_SCALE, 0.5));
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    [self.titleImage sd_setImageWithURL:[NSURL URLOffsetIPWithString:model[@"s_logo"]]];
    self.titleLabel.attributedText = [self getAttributeName:model[@"s_name"] one:model[@"describe"] second:model[@"s_praise"] three:model[@"s_popularity"] four:model[@"s_distribution"]];
}

- (NSAttributedString *)getAttributeName:(NSString *)name one:(NSString *)one second:(NSString *)second three:(NSString *)three four:(NSString *)four{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *oneStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"商品描述:%@",one]];
    NSAttributedString *secondAtt = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"+%@",second] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *threeStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"+%@",three] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *fourAtt = [[NSAttributedString alloc]initWithString:four attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:oneStr];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"好评"]];
    [attStr appendAttributedString:secondAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"/ "]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"人气"]];
    [attStr appendAttributedString:threeStr];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"是否配送:"]];
    [attStr appendAttributedString:fourAtt];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

@end
